package com.shanwen.select.web;

import com.shanwen.select.entity.Menu;
import com.shanwen.select.entity.Role;
import com.shanwen.select.entity.RoleMenu;
import com.shanwen.select.service.AdminUserService;
import com.shanwen.select.service.MenuService;
import com.shanwen.select.service.RoleService;
import org.apache.shiro.authz.annotation.RequiresRoles;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.*;

@Controller
@RequestMapping("/role")
public class RoleController {
    @Autowired
    MenuService menuService;
    @Autowired
    RoleService roleService;
    @Autowired
    AdminUserService userService;

    @RequestMapping(value = "/list", method = RequestMethod.GET)
    @RequiresRoles("admin")
    public String roleList(Model model, Role role) {

        List<Role> roles = roleService.list();
        model.addAttribute("roleList", roles);
        return "/role/roleList";
    }

    @RequestMapping(value = "/add", method = RequestMethod.GET)
    @RequiresRoles("admin")
    public String roleAddView(Model model) {
        List<Menu> menus = menuService.list();
        model.addAttribute("menuList", menus);
        return "/role/roleAdd";
    }

    @RequestMapping(value = "/add", method = RequestMethod.POST)
    @RequiresRoles("admin")
    public String roleAdd(Model model, Integer[] nodes, Role role) {

        role.setCreateDate(new Date());
        //保存角色
        Map<String, Object> map = new HashMap<>();
        roleService.insertRole(role);
        List<RoleMenu> list = new ArrayList<>();
        for (Integer auth : nodes) {
            if (auth != null) {
                RoleMenu roleMenu = new RoleMenu();
                roleMenu.setMenuId(auth);
                roleMenu.setRoleId(role.getId());
                list.add(roleMenu);
            }
        }
        roleService.insertRoleMenu(list);
        return "redirect:list";
    }

    @RequestMapping(value = "/edit", method = RequestMethod.GET)
    @RequiresRoles("admin")
    public String rolemodify(Model model, Role role) {
        role = roleService.getById(role.getId());

        model.addAttribute("role", role);

        return "role/roleEdit";
    }

    @RequestMapping(value = "/edit", method = RequestMethod.POST)
    @RequiresRoles("admin")

    public String modify(Role role, Integer[] nodes) {
        // role = roleService.getRole(role);
        roleService.updateById(role);
        if (nodes.length != 0) {
            roleService.delete(role.getId());
            List<RoleMenu> list = new ArrayList<>();
            for (Integer auth : nodes) {
                if (auth != null) {
                    RoleMenu roleMenu = new RoleMenu();
                    roleMenu.setMenuId(auth);
                    roleMenu.setRoleId(role.getId());
                    list.add(roleMenu);
                }
            }
            roleService.insertRoleMenu(list);
        }

        return "redirect:list";
    }


    @RequestMapping("/getMenu")
    @ResponseBody
    public List<Menu> getMenu(Integer id) {

        if (id != null) {
            List<Menu> list = menuService.getMenuList(id);
            return list;
        } else {
            List<Menu> list = menuService.list();
            return list;
        }
    }
}
